package com.ksyzt.ksite2.client.column;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageComposite;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.modle.ATTACHMENTObj;
import com.ksyzt.ksite2.shared.modle.ATTACHMENTObjs;

public class TagsEditor extends MessageComposite {

	private static TagsEditorUiBinder uiBinder = GWT
			.create(TagsEditorUiBinder.class);

	interface TagsEditorUiBinder extends UiBinder<Widget, TagsEditor> {
	}

	public TagsEditor() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiField
	Grid panel;
	@UiField
	HorizontalPanel panelSystem;

	@UiField
	Grid custom;

	List<String> m_tags;
	private AsyncCallback<ATTACHMENTObjs> m_on_tags = new AsyncCallback<ATTACHMENTObjs>() {

		@Override
		public void onSuccess(ATTACHMENTObjs arg0) {

			renderTags(arg0);
		}

		@Override
		public void onFailure(Throwable arg0) {
			panel.clear();

		}
	};

	public void initTags(String nid, String tags) {
		txtTag.setValue("");
		m_tags = new ArrayList<String>();
		if (tags == null || tags.equals("")) {

		} else {
			tags = tags.trim();
			String[] s = tags.split(",");
			for (int i = 0; i < s.length; i++) {
				m_tags.add(s[i]);
			}
		}
		if (nid != null) {
			ServerProxy.SYS_MANAGER.getAttatchments(nid, m_on_tags);
		} else {
			renderTags(null);
		}
	}

	protected void renderTags(ATTACHMENTObjs arg0) {

		if (arg0 == null) {
			arg0 = new ATTACHMENTObjs();
		}
		panel.clear();
		int colcount = 5;
		int row;
		int col;
		panel.resize((int) Math.floor(arg0.size() / colcount)
				+ (arg0.size() % colcount == 0 ? 0 : 1), colcount);
		row = 0;
		col = 0;
		if (arg0 != null) {
			for (ATTACHMENTObj a : arg0) {
				ToggleButton b = new ToggleButton(a.getTITLE());
				b.setDown(false);
				panel.setWidget(row, col++, b);
				if (col >= colcount) {
					row++;
					col = 0;
				}

				for (int i = 0; i < m_tags.size(); i++) {
					String tag = m_tags.get(i);
					if (tag.equals(a.getTITLE())) {
						b.setDown(true);
						m_tags.remove(i);
						break;
					}
				}
			}
		}

		for (int k = 0; k < panelSystem.getWidgetCount(); k++) {
			ToggleButton b0 = (ToggleButton) panelSystem.getWidget(k);
			for (int i = 0; i < m_tags.size(); i++) {
				String tag = m_tags.get(i);
				if (tag.equals(b0.getText())) {
					b0.setDown(true);
					m_tags.remove(i);
					break;
				}
			}
		}

		custom.clear();
		custom.resize(
				(int) Math.floor(m_tags.size() / colcount)
						+ (m_tags.size() % colcount == 0 ? 0 : 1), colcount);
		row = 0;
		col = 0;
		for (int j = 0; j < m_tags.size(); j++) {
			ToggleButton b = new ToggleButton(m_tags.get(j));
			b.setDown(true);

			custom.setWidget(row, col++, b);
			if (col >= colcount) {
				row++;
				col = 0;
			}
		}

	}

	@UiHandler("btnCancel")
	void onCancel(ClickEvent e) {
		MessageEvent ev = new MessageEvent(MessageEvent.CANCEL, 0);
		fireEvent(ev);
	}

	@UiHandler("btnOK")
	void onOK(ClickEvent e) {
		String tags = getSelectTags();
		MessageEvent ev = new MessageEvent(MessageEvent.OK, tags);
		fireEvent(ev);
	}

	private String getSelectTags() {
		List<String> list = new ArrayList<String>();

		for (int k = 0; k < panelSystem.getWidgetCount(); k++) {
			ToggleButton b0 = (ToggleButton) panelSystem.getWidget(k);
			if (b0.getValue() == true) {
				list.add(b0.getText());
			}
		}

		for (int k = 0; k < panel.getRowCount(); k++) {
			for (int m = 0; m < panel.getColumnCount(); m++) {
				ToggleButton b0 = (ToggleButton) panel.getWidget(k, m);
				if (b0 != null && b0.getValue() == true) {
					list.add(b0.getText());
				}
			}
		}

		for (int k = 0; k < custom.getRowCount(); k++) {
			for (int m = 0; m < panel.getColumnCount(); m++) {
				ToggleButton b0 = (ToggleButton) custom.getWidget(k, m);
				if (b0 != null && b0.getValue() == true) {
					list.add(b0.getText());
				}
			}
		}

		String text = txtTag.getValue();
		if (text != null && text.length() > 0) {
			text = text.trim();
			if (text.length() > 0) {
				text = text.replace(" ", ",");
				String[] ls = text.split(",");
				for (String s : ls) {
					list.add(s);
				}
			}
		}

		StringBuilder sb = new StringBuilder();
		if (list.size() > 0) {
			sb.append(list.get(0));
			for (int i = 1; i < list.size(); i++) {
				String l = list.get(i);
				l = l.trim();
				if (l.length() > 0) {
					sb.append(",").append(l);
				}
			}
		}
		return sb.toString();
	}

	@UiField
	TextBox txtTag;
}
